
//
//  UILabel+testSize.m
//  BadBoy
//
//  Created by wen on 2017/3/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "UILabel+testSize.h"

@implementation UILabel (testSize)

- (void)setTest:(NSString *)test font:(UIFont *)font frameStyle:(YYFrameStyle)frameStyle horizontal:(CGFloat)horizontal vertical:(CGFloat)vertical widthOffset:(CGFloat)widthOffset{
    [self setTest:test font:font frameStyle:frameStyle horizontal:horizontal vertical:vertical widthOffset:widthOffset height:0];
}

- (void)setTest:(NSString *)test font:(UIFont *)font frameStyle:(YYFrameStyle)frameStyle horizontal:(CGFloat)horizontal vertical:(CGFloat)vertical widthOffset:(CGFloat)widthOffset height:(CGFloat)height{
    self.text = test;
    self.font = font;
    self.frame = [self getFrameWithTest:test font:font frameStyle:frameStyle horizontal:horizontal vertical:vertical widthOffset:widthOffset height:height];
}

- (CGRect)getFrameWithTest:(NSString *)test font:(UIFont *)font frameStyle:(YYFrameStyle)frameStyle horizontal:(CGFloat)horizontal vertical:(CGFloat)vertical widthOffset:(CGFloat)widthOffset height:(CGFloat)height{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    CGSize size = [test boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
    CGFloat width = size.width + widthOffset;
    height = height == 0 ? size.height : height;
    CGFloat orginX = [self orginXWithWidth:width horizontal:horizontal frameStyle:frameStyle];
    CGFloat orginY = [self orginYWithHeight:height vertical:vertical frameStyle:frameStyle];
    CGRect rect = CGRectMake(orginX, orginY, width, height);
    return rect;
}

- (CGFloat)orginXWithWidth:(CGFloat)width horizontal:(CGFloat)horizontal frameStyle:(YYFrameStyle)frameStyle{
    switch (frameStyle) {
        case YYFrameStyleLeftAndTop:
        case YYFrameStyleLeftAndBottom:
        case YYFrameStyleLeftAndCenterY:
            return horizontal;
            break;
        
        case YYFrameStyleRightAndTop:
        case YYFrameStyleRightAndBottom:
        case YYFrameStyleRightAndCenterY:
            return horizontal - width;
            break;
            
        case YYFrameStyleCenterXAndTop:
        case YYFrameStyleCenterXAndBottom:
        case YYFrameStyleCenterXAndCenterY:
            return horizontal - width / 2.0;
            break;
        default:
            break;
    }
    return 0;
}

- (CGFloat)orginYWithHeight:(CGFloat)height vertical:(CGFloat)vertical frameStyle:(YYFrameStyle)frameStyle{
    switch (frameStyle) {
        case YYFrameStyleCenterXAndTop:
        case YYFrameStyleRightAndTop:
        case YYFrameStyleLeftAndTop:
            return vertical;
            break;
        case YYFrameStyleCenterXAndCenterY:
        case YYFrameStyleRightAndCenterY:
        case YYFrameStyleLeftAndCenterY:
            return vertical - height / 2.0;
            break;
        case YYFrameStyleCenterXAndBottom:
        case YYFrameStyleRightAndBottom:
        case YYFrameStyleLeftAndBottom:
            return vertical - height;
            break;
        default:
            break;
    }
    return 0;
}
@end
